package com.example.springvue.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.springvue.mapper.AuthRuleMapper;
import com.example.springvue.model.AuthRule;
import com.example.springvue.service.AuthRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@Service
public class AuthRuleServiceImpl implements AuthRuleService {
    @Resource
    private AuthRuleMapper authRuleMapper;

    @Override
    public List<Map< String, Object >> getAuthRuleList(String Id) {
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRuleList(Id);
        return authRule;
    }

    @Override
    public List<Map< String, Object >> getAuthRule() {
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRule();
        return authRule;
    }

    @Override
    public List<Map< String, Object >> getAuthRuleListTwo(String Id) {
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRuleListTwo(Id);
        return authRule;
    }

    @Override
    public List<Map< String, Object >> getAuthRuleTwo(String Id) {
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRuleTwo(Id);
        return authRule;
    }



    @Override
    public List<Map< String, Object >> getAuthRuleCascade() {
        List<Map< String, Object >> list = new ArrayList<>();
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRule();
        for (int i =0 ; i<authRule.size();i++){
            Map< String, Object > info = new HashMap<String, Object>();
            info.put("value",authRule.get(i).get("id"));
            info.put("pid",authRule.get(i).get("pid"));
            info.put("label",authRule.get(i).get("title"));
            String pid = String.valueOf((Long) authRule.get(i).get("id"));
            List<Map< String, Object >> children = getAuthRuleCascadeTrre(pid);
            if (CollectionUtils.isNotEmpty(children)) {
                info.put("children",children);
            }
            list.add(info);
        }
        return list;
    }



    public List<Map< String, Object >> getAuthRuleCascadeTrre(String id) {
        List<Map< String, Object >> list = new ArrayList<>();
        List<Map< String, Object >> authRule = authRuleMapper.getAuthRuleListTwo(id);
        for (int i =0 ; i<authRule.size();i++){
            Map< String, Object > info = new HashMap<String, Object>();
            info.put("value",authRule.get(i).get("id"));
            info.put("pid",authRule.get(i).get("pid"));
            info.put("label",authRule.get(i).get("title"));
            String pid = String.valueOf((Long) authRule.get(i).get("id"));
            List<Map< String, Object >> children = getAuthRuleListTwo(pid);
            if (CollectionUtils.isNotEmpty(children)) {
                info.put("children",children);
            }
            list.add(info);
        }
        return list;
    }


    @Override
    public boolean authRuleAdd(Map<String, Object> map) {
        AuthRule authRule =new AuthRule();
        authRule.setStatus((Integer) map.get("status"));
        authRule.setPid((Integer) map.get("pid"));
        authRule.setUrl((String) map.get("url"));
        authRule.setTitle((String) map.get("title"));
        authRule.setIcon((String) map.get("icon"));
        authRule.setComponent((String) map.get("component"));
        authRule.setModId((Integer) map.get("mod_id"));
        authRule.setRemark((String) map.get("remark"));
        authRule.setRedirect((String) map.get("redirect"));
        authRule.setSort((Integer) map.get("sort"));
        authRule.setIsMenu((Integer) map.get("is_menu"));
        authRule.setCreatetime(new Timestamp(System.currentTimeMillis()));
        authRule.setUpdatetime(new Timestamp(System.currentTimeMillis()));

        int add = authRuleMapper.insert(authRule);
        if (add ==1){
            return true;
        }else {
            return false;
        }
    }


    @Override
    public boolean authRuleEdit(Map<String, Object> map) {
        AuthRule authRule =new AuthRule();
        authRule.setId((Integer) map.get("id"));
        authRule.setStatus((Integer) map.get("status"));
        authRule.setPid((Integer) map.get("pid"));
        authRule.setUrl((String) map.get("url"));
        authRule.setTitle((String) map.get("title"));
        authRule.setIcon((String) map.get("icon"));
        authRule.setComponent((String) map.get("component"));
        authRule.setModId((Integer) map.get("mod_id"));
        authRule.setRemark((String) map.get("remark"));
        authRule.setRedirect((String) map.get("redirect"));
        System.out.print(map.get("sort").getClass());
        authRule.setSort((Integer) map.get("sort"));
        authRule.setIsMenu((Integer) map.get("is_menu"));
        authRule.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        int edit = authRuleMapper.updateById(authRule);
        if (edit ==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean authRuleDelete(String id) {
        Integer Delete = authRuleMapper.deleteById(id);
        System.out.println(Delete);
        if (Delete ==1){
            return true;
        }else {
            return false;
        }
    }

}
